package com.shengdong.water.feature.notice

import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.kongzue.dialogx.dialogs.WaitDialog
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.shengdong.water.R
import com.shengdong.water.base.RefreshLoadStatus
import com.shengdong.water.code.adapter.NotifyMsgAdapter
import com.shengdong.water.code.base.BaseRecyclerAdapter
import com.shengdong.water.code.base.BaseRefreshActivity
import com.shengdong.water.code.bean.NotifyMsg
import com.shengdong.water.code.config.RouterConfig
import com.shengdong.water.code.viewmodel.NoticeViewModel
import com.shengdong.water.code.viewmodel.PageListViewModel
import com.shengdong.water.databinding.ActivityNoticeBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch


@Route(path = RouterConfig.NOTICE_PATH)
@AndroidEntryPoint
class NoticeActivity : BaseRefreshActivity<NotifyMsg, ActivityNoticeBinding, NoticeViewModel>(){


    override fun getLayoutId() = R.layout.activity_notice

    override fun getSmartRefreshLayout() = binding.refresh

    override fun getRecyclerView() = binding.recyclerNotice

    override fun getViewModelClass() = NoticeViewModel::class.java

    override fun getAdapter() = NotifyMsgAdapter()


    override fun initViews() {
        super.initViews()
        setTitle("水质报警通知")
        binding.viewModel = viewModel
    }

    override fun initData() {
        super.initData()
        loadData()
    }

    override fun loadData() {

        viewModel.getNotify(currentPage, pageSize)

    }

    override fun observeData() {
        super.observeData()
        lifecycleScope.launch {
                println("NoticeActivity: 开始设置数据收集")
                repeatOnLifecycle(Lifecycle.State.STARTED) {
                    launch {
                        viewModel.notifyMsg.collect { notifyMsg ->
                            println("NoticeActivity: 收到数据 currentPage=$currentPage, data=$notifyMsg")
                            notifyMsg.let {
                                if(currentPage == 1 && it.isEmpty()){
                                    binding.recyclerNotice.setEmptyView(binding.layoutEmpty.root)
                                    return@collect
                                }

                                if (it.isEmpty()) {
                                    finishRefreshAndLoadMore(true, currentPage)
                                    return@collect
                                }
                                handleData(it)
                            }
                        }

                    }
                }


        }
    }

    override fun showLoading(loading: String) {
        super.showLoading(loading)
        WaitDialog.show(loading)
    }

    override fun dismissLoading() {
        super.dismissLoading()
        WaitDialog.dismiss()
    }

}